<template>
  <div>

    <div class="login_layout">

    <el-card class="box-card">

      <el-container style="border-radius: 5px;">

        <el-header>

          <h2>登录</h2>

          <p>高校智慧云会议系统</p>

        </el-header>

        <el-main style="margin-top: 25px;">

          <div>

            <el-form label-position="right" label-width="0px" style="margin-top: 20px;max-width: 400px" class="loginForm">

              <el-form-item>

                <el-input size="large" placeholder="手机号" prefix-icon="Iphone" v-model="phone" />

              </el-form-item>

              <el-form-item >

                <el-input size="large" placeholder="验证码" maxlength="6" prefix-icon="Lock" type="text" v-model="code" >

                  <template #suffix>
                    <el-button @click="getCode" style="border: none;background:none">
                      <span v-show="!isClick" style="color:var(--el-color-primary);">发送验证码</span>
                      <span v-show="isClick" style="color: var(--el-color-primary-light-3);">发送验证码</span>
                    </el-button>
                  </template>
                
                </el-input>

              </el-form-item>

              <el-row style="margin-top: 20px">

                <el-button style="margin-top: 30px" type="primary" class="btn-grad" @click="login">登 录</el-button>

              </el-row>
              <el-row class="row-container">

                <el-col :span="12" class="left-col">
                  <a class="toReg" href="#/register"></a>
                </el-col>
                
                <el-col :span="12" class="right-col">
                  <a class="login" href="#/">密码登录</a>
                </el-col>

              </el-row>

            </el-form>

          </div>

        </el-main>

      </el-container>

    </el-card>
  </div>
  </div>
</template>

<script>
import Qs from 'qs'
import { ElMessage } from 'element-plus'
import router from "@/router";
export default {
  created: function() {},
  mounted:function(){
        const script = document.createElement('script')
        script.type = 'text/javascript';
        script.src ='http://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js';
        document.body.appendChild(script);
  },
  data: function() {
    return {
      phone: '',
      code: '',
      isAgree: false,
      isClick: false
    }
  },
  methods: {
    getCode(){
      if (this.phone === "") {
        ElMessage.error("手机号不能为空！");
      } else {
        this.isClick=true;
        let data = {
          phone: this.phone,
        }
        data = Qs.stringify(data);
        this.$http.post("/user/security/code", data)
            .then((response) => {
              switch (response.data.code) {
                case 200:
                  ElMessage({ message: '发送成功！', type: 'success' });
                  break;
                default:
                  ElMessage.error(response.data.msg);
              }
            });
      }
    },
    login() {
      if (this.phone === "") {
        ElMessage.error("手机号不能为空！");
      } else {
        let data = {
          phone: this.phone,
          code: this.code
        }
        data = Qs.stringify(data);
        this.$http.post("/user/security/login/phone", data)
            .then((response) => {
              switch (response.data.code) {
                case 200:
                  ElMessage({ message: '登录成功！', type: 'success' });
                  router.push('/main/university/manage');
                  localStorage.setItem("token",response.data.data.token);
                  this.$store.commit("setUser",response.data.data);
                  break;
                default:
                  ElMessage.error(response.data.msg);
              }
            });
      }
    }
  }
}
</script>
<style scoped>
.login_layout {
  position: absolute;
  left: calc(50% - 200px);
  top: 15%;
  width: 400px;
  height: 500px;
}

.loginForm {
  text-align: center;
}

.checkBox {
  margin-left: 7px;
}

.btn-grad {
  padding: 15px 45px;
  height: 40px;
  text-align: center;
  text-transform: uppercase;
  transition: 0.5s;
  background-size: 200% auto;
  color: white;
  box-shadow: 0 0 10px rgb(128, 128, 128);
  border-radius: 13px;
  display: block;
  border: none;
  width: 400px;
}

.btn-grad:hover {
  background-position: right center;
  /* change the direction of the change here */
  color: #fff;
  text-decoration: none;
}

.row-container {
  display: flex;
  justify-content: space-between;
}

.left-col {
  text-align: left;
}

.right-col {
  text-align: right;
}

.toReg {
  float: left;
  margin-top: 10px;
  color: var(--el-color-primary);
  text-decoration: none;
}

.toReg:hover {
  color: var(--el-color-primary-light-3);
}

.login {
  float: right;
  margin-top: 10px;
  color: var(--el-color-primary);
  text-decoration: none;
}

.login:hover {
  color: var(--el-color-primary-light-3);
}
</style>
  
  